package com.cn.springcloudwechat.serviceImpl.vaccinstation;

import com.cn.springcloudwechat.mapper.mapper.vaccine.YmChildrenOrderVaccinationMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccine.YmVaccineMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccine.YmVaccineTypeMapper;
import com.cn.springcloudwechat.mapper.mapper.vaccinestation.YmStationVaccineMapper;
import com.cn.springcloudwechat.model.YmChildrenOrderVaccination;
import com.cn.springcloudwechat.model.YmStationVaccine;
import com.cn.springcloudwechat.model.vaccine.YmVaccine;
import com.cn.springcloudwechat.model.vaccine.YmVaccineType;
import com.cn.springcloudwechat.service.vaccinstation.YmStationVaccineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 疫苗服务站关联关系serviceImpl
 * @author: Administrator
 * @create: 2018/8/31 14:45
 */

@Service
public class YmStationVaccineServiceImpl implements YmStationVaccineService {
    @Autowired
    YmStationVaccineMapper ymStationVaccineMapper;
    @Autowired
    YmVaccineTypeMapper ymVaccineTypeMapper;
    @Autowired
    YmVaccineMapper ymVaccineMapper;

    @Autowired
    YmChildrenOrderVaccinationMapper ymChildrenOrderVaccinationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSelective(YmStationVaccine record) {
        ymStationVaccineMapper.insertSelective(record);
    }

    @Override
    public YmStationVaccine selectByPrimaryKey(Long stationVaccineId) {
        return ymStationVaccineMapper.selectByPrimaryKey(stationVaccineId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByPrimaryKeySelective( List<Long>list,Long stationId) {
        List<YmVaccineType> oldYmVaccineType= ymVaccineTypeMapper.getVaccineTypeByStationId(stationId,null);
        List<Long> oldTypeId=new ArrayList<Long>();
        for (YmVaccineType ymVaccineType:oldYmVaccineType) {
            oldTypeId.add(ymVaccineType.getVaccineTypeId());
        }

        List<Long> oldDifferenceSetArticleIds=   removeAll(oldTypeId,list);
        if(oldDifferenceSetArticleIds.size()>0){
            ymStationVaccineMapper.updateByPrimaryKeySelective(oldDifferenceSetArticleIds,stationId,(short)0);
        }
        System.out.println();
        List<Long> newDifferenceSetArticleIds = removeAll(list, oldTypeId);
        if(newDifferenceSetArticleIds.size()>0){
            ymStationVaccineMapper.insertYmStationVaccineBatch(stationId,newDifferenceSetArticleIds);
        }

    }


    /**
     * 获取集合差集
     *
     * @param src
     * @param target
     * @return
     */
    private static List<Long> removeAll(List<Long> src, List<Long> target) {
        //大集合用linkedlist
        LinkedList<Long> result = new LinkedList<>(src);
        //小集合用hashset
        HashSet<Long> targetHash = new HashSet<>(target);
        //采用Iterator迭代器进行数据的操作
        Iterator<Long> iter = result.iterator();
        while (iter.hasNext()) {
            if (targetHash.contains(iter.next())) {
                iter.remove();
            }
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertYmStationVaccineBatch(Long stationId, List<Long> list) {
        ymStationVaccineMapper.insertYmStationVaccineBatch(stationId,list);
    }

    @Override
    public List<YmVaccineType> getVaccineTypeByStationId(Long stationId,String vaccineName) {
        return ymVaccineTypeMapper.getVaccineTypeByStationId(stationId,vaccineName);
    }

    @Override
    public List<YmVaccine> getYmVaccineByVaccineTypeId(Long childId,Long vaccineTypeId) {
      int agent = ymVaccineMapper.getChildeAgent(childId, vaccineTypeId);
      List<YmVaccine> result = ymVaccineMapper.getVaccineMapperByParameter(vaccineTypeId,agent);
      return result;
    }

    @Override
    public void updateUserOrderState(Long orderVaccinationId) {
        ymChildrenOrderVaccinationMapper.updateUserOrderState(orderVaccinationId);
    }

    @Override
    public List<Map<String,Object>> queryVaccineOrderPlanAllMapper(Long childId) {
        return  ymVaccineMapper.queryVaccineOrderPlanAllMapper(childId);
    }

    @Override
    public Integer getRowNumber(String orderDate,Long stationID) {
        return ymChildrenOrderVaccinationMapper.getRowNumber(orderDate,stationID);
    }

}
